package com.yj.springbootevent.rest;

import com.yj.springbootevent.dto.UserDto;
import com.yj.springbootevent.event.MyApplicationEvent;
import com.yj.springbootevent.event.MyApplicationEventPublisher;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class TestController {


    @Autowired
    MyApplicationEventPublisher myApplicationEventPublisher;

    @Autowired
    ApplicationContext applicationContext;

    @GetMapping("send")
    public String send(){

        UserDto userDto = new UserDto();
        userDto.setId(1);
        userDto.setName("zs");
        userDto.setAddress("zzz");

        //方式一
//        myApplicationEventPublisher.publishEvent(userDto);

        //方式二
        applicationContext.publishEvent(new MyApplicationEvent(this,userDto));

        return "success";
    }

}
